package assignment.ton.def;

import java.awt.Color;
import java.awt.Component;
import java.awt.geom.Point2D;

import javax.swing.JOptionPane;

import assignment.ton.gui.MainFrame;
/**
 * Class for static reusable methods
 */
public class Util {
	/**
	 * Opens of a are you sure prompt
	 * @return true if yes is pressed
	 */
	public static boolean areYouSure(){
		if (JOptionPane.showConfirmDialog(null, "Are you sure?", "Are you sure?", 
			    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
			    == JOptionPane.YES_OPTION)
			{
				return true;
			}
			else
			{
				return false;
			}
	}
	public static boolean areYouSure(String message){
		if (JOptionPane.showConfirmDialog(null, message, "Are you sure?", 
			    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
			    == JOptionPane.YES_OPTION)
			{
				return true;
			}
			else
			{
				return false;
			}
	}
	public static boolean areYouSure(String message, String title){
		if (JOptionPane.showConfirmDialog(null, message, title, 
			    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
			    == JOptionPane.YES_OPTION)
			{
				return true;
			}
			else
			{
				return false;
			}
	}
	/**
	 * Asks are you sure and if so, quits
	 */
	public static void quit(){
		if(Util.areYouSure()) System.exit(0);
	}
	/**
	 * For if you can change selection when an actions happening
	 * @param a the action in question
	 * @return true if you're allowed to change
	 */
	public static boolean canChange(Action a){
		boolean rVal = false;
		if(a==null){ return true; }
		switch(a){
		case NONE: case CLASSSELECTED: case DRAGGING:
			rVal = true;
		}
		return rVal;
	}
	/**
	 * Checks to see whether v is of type t
	 * @param t the type string ie "int", "String"
	 * @param v the String to analyse
	 * @return true if it is of type
	 */
	public static boolean checkType(String t, String v){
		if(t.equals("String")){
			return true;
		}else if(t.equals("int")){
			try{
				Integer.parseInt(v);
				return true; 
			}catch(NumberFormatException e){
				return false;
			}
		}else if(t.equals("double")){
			try{
				Double.valueOf(v);
				return true; 
			}catch(NumberFormatException e){
				return false;
			}
		}else if(t.equals("long")){
			try{
				Long.valueOf(v);
				return true; 
			}catch(NumberFormatException e){
				return false;
			}
		}else if(t.equals("char")){
			if(v.length()>1){ return false; }
			try{
				v.charAt(0);
				return true; 
			}catch(IndexOutOfBoundsException e){
				return false;
			}
		}else if(t.equals("float")){
			try{
				Float.valueOf(v);
				return true; 
			}catch(NumberFormatException e){
				return false;
			}
		}
		return false;
	}
	/**
	 * Checks if an action can be cancelled through right click
	 * @param a the action in question
	 * @return true if it is allowed
	 */
	public static boolean canCancel(Action a){
		boolean rVal = false;
		if(a==null){ return true; }
		switch(a){
		case NONE: case CLASSSELECTED: case DRAGGING: 
		case ADDCLASS: case ADDLINK: case DELETELINK: case EDITLINK:
			rVal = true;
		}
		return rVal;
	}
	/**
	 * Shows an error option pain
	 * @param a a JComponent to center to
	 * @param error string error
	 */
	public static void showError(Component a, String error){
		String eT = "Error!";
		JOptionPane.showMessageDialog(a, error, eT, JOptionPane.ERROR_MESSAGE);
	}
	public static void showWarning(Component a, String error){
		String eT = "Warning!";
		JOptionPane.showMessageDialog(a, error, eT, JOptionPane.ERROR_MESSAGE);
	}
	public static void showInfo(Component a, String error){
		String eT = "Information";
		JOptionPane.showMessageDialog(a, error, eT, JOptionPane.ERROR_MESSAGE);
	}
	/**
	 * Converts an action enum to a readable string
	 * @param a the action in question
	 * @param mainFrame needs the main frame object to get the currently selected class
	 * @return the readable action
	 */
	public static String actionToString(Action a, MainFrame mainFrame){
		String name = "";
		if(mainFrame.getSelected()!=null){
			name = " "+mainFrame.getSelected().getSensibleName();
		}
		switch(a){
		case ADDCLASS: case ADDCLASS2:
			return "Add Class";
		case ADDLINK: case ADDLINK2:
			return "Add Link";
		case EDITCLASS:
			return "Edit Class";
		case CLASSSELECTED:
			return "Class Selected "+name;
		case DRAGGING:
			return "Dragging "+name;
		case ADDCONSTRUCTOR:
			return "Add Constructor";
		case DELETELINK:
			return "Delete Link";
		case EDITLINK: case EDITLINK2:
			return "Edit Link";
		case VARIABLES:
			return "Editing Variables";
		case METHODS:
			return  "Editing Methods";
		case EDITCODE:
			return "Editing Code";
		default:
			return "None";
		}
	}
	/**
	 * @return array of allowed types of data
	 */
	public static String[] getDataTypes(){
		String [] data;
		data = new String[6];
		data[0] = "String";
		data[1] = "int";
		data[2] = "double";
		data[3] = "long";
		data[4] = "char";
		data[5] = "float";
		return data;
	}
	/**
	 * Small method to merge string arrays
	 * @param a the first array
	 * @param b the second string array
	 * @return the merged array
	 */
	public static String[] mergeStringArrays(String[] a, String[] b){
		String [] outArray = new String[a.length+b.length];
		for(int i = 0; i<a.length; i++){
			outArray[i] = a[i];
		}
		for(int i = 0; i<b.length; i++){
			outArray[i+a.length] = b[i];
		}
		return outArray;
	}
	/**
	 * 
	 * @param r1 Top left corner of the rectangle
	 * @param r2 Bottom right corner of rectangle
	 * @param m Position of mouse
	 * @return true if its in the rectangle
	 */
	public static boolean isInRectnalge(Point2D r1, Point2D r2, Point2D m){
		int x1 = (int)r1.getX();
		int x2 = (int)r2.getX();
		int mx = (int)m.getX();
		int y1 = (int)r1.getY();
		int y2 = (int)r2.getY();
		int my = (int)m.getY();
		if(x1<=mx&&mx<=x2&&y1<=my&&my<=y2){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * @return the background colour main
	 */
	public static Color background(){
		return new Color(191,228,255);
	}
	/**
	 * @return the background colour for the draw panel
	 */
	public static Color backgroundRight(){
		return new Color(225,244,255);
	}
}
